package pl.nazaweb.samplesandtutorials;

import java.util.ArrayList;
import java.util.List;

import pl.nazaweb.samplesandtutorials.abstracts.ShopList;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import pl.nazaweb.samplesandtutorials.adapters.*;

public class ListViewExample extends Activity {

	ListView list;
	List<ShopListItem> itemList = new ArrayList();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_view_example);
		initShopList();
		ShopListAdapter adapter = new ShopListAdapter(this, itemList);
		list = (ListView) findViewById(R.id.sho_item_list_view);
		list.setAdapter(adapter);
	}

	private void initShopList() {
		ShopList shopList = new ShopList();
		Uri allShopItems = shopList.getContentUri();
		Cursor cur = managedQuery(allShopItems, null, null, null, null);
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			ShopListItem item = new ShopListItem(cur.getString(1),
					cur.getDouble(cur.getColumnIndex("price")));
			itemList.add(item);
			Log.v("item", item.toString());
		}
		cur.close();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_list_view_example, menu);
		return true;
	}
}
